<?php

namespace App\Model;

/**
 * Class Order
 * @package App\Model
 * @User wuqin
 * @Date 2018/4/10 0010
 * @Time 17:56
 */
class Order extends Common
{
    /**
     * @var bool
     */
//    public $timestamps = false;
    /**
     * @var string
     */
    protected $table = "orders";
    /**
     * @var string
     */
    protected $primaryKey = 'order_id';

    /**
     * 方法 : add_order
     * @User wuqin
     * @DateTime 2018-04-10 17:56
     * @param $var
     * @return Order|bool
     */
    public function add_order($var)
    {
        $this->user_id = $var['user_id'];
        $this->commodity_id = $var['commodity_id'];
        $this->info_id = $var['info_id'];
        $this->color_id = $var['color_id'];
        $this->size_id = $var['size_id'];
        $this->number = $var['number'];
        $this->unit_price = $var['unit_price'];
        $this->all_price = $var['all_price'];
        $this->parent_order_id = $var['parent_order_id'];
        $this->state = 0;

        return $this->save() ? $this : false;
    }

    /**
     * 方法 : update_order
     * @User wuqin
     * @DateTime 2018-04-10 17:56
     * @param $var
     * @return Order|bool
     */
    public function update_order($var)
    {

        $this->user_id = $var['user_id'];
        $this->commodity_id = $var['commodity_id'];
        $this->info_id = $var['info_id'];
        $this->color_id = $var['color_id'];
        $this->size_id = $var['size_id'];
        $this->number = $var['number'];
        $this->unit_price = $var['unit_price'];
        $this->all_price = $var['all_price'];
        $this->state = $var['state'];

        return $this->save() ? $this : false;
    }

    public function getOrderByParent($parent_order_id)
    {
        if (empty($parent_order_id))
            return false;

        $where = array(
            array("where", "parent_order_id", "=", $parent_order_id)
        );

        $res = $this->get($where, "*", 0, 0, ["created_at", "asc"]);
        if (empty($res))
            return false;
        else
            return $res;
    }

    public function parent_orders()
    {
        return $this->belongsTo('App\Model\Parent_order', "parent_order_id");
    }

    public function parent_order()
    {
        return $this->belongsTo(Parent_order::class, "parent_order_id");
    }

}
